using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Voice.Nomination;
using Ent.Voice.Organisation;
using Ent.Web.Helper;
using System.Collections.Generic;
public partial class ShowComment : System.Web.UI.Page
{
    int count = 5;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateCommments();
        }
    }
    void PopulateCommments()
    {
        try
        {
            int id = MathFunction.ConvertToInteger(Request.QueryString["id"], 0);
            int pi = MathFunction.ConvertToInteger(Request.QueryString["pi"], 0);
            pi = pi == 0 ? 1 : pi;
            int start = (pi - 1) * count + 1;

            int total = 1;
            DataTable dt = GetVote(id, ref total, start);
            int pageCount = dt.Rows.Count;
            lblHeader.Text = ResourceHelper.Get("MSG_SearchHeader") + " " + start.ToString() + " to " + Convert.ToInt32(start + pageCount - 1) + " of " + total.ToString() + " records.";
            SetNavigation(pi, total, start);
            if (dt != null && dt.Rows.Count != 0)
            {
                dgCommentList.DataSource = dt;
                dgCommentList.DataBind();
            }
            else
            {
                lblHeader.Text = Common.GetAlertText(ResourceHelper.Get("MSG_NoMessage"));
                dgCommentList.Visible = false;
            }
        }
        catch (Exception exc)
        {
            Common.ErrorPage(exc);
        }
    }
    private void SetNavigation(int pi, int total, int start)
    {
        Dictionary<string, string> kvList = new Dictionary<string, string>();
        kvList.Add("pi", "");
        btnPrev.Visible = false;
        btnNext.Visible = false;
        if (pi > 1)
        {
            btnPrev.Visible = true;
            kvList["pi"] = Convert.ToString(pi - 1);
            btnPrev.Attributes.Add("onclick", "javascript:window.location.href='" + Common.ResetUrl(kvList) + "'");
        }
        if (total >= start + count)
        {
            btnNext.Visible = true;
            kvList["pi"] = Convert.ToString(pi + 1);
            btnNext.Attributes.Add("onclick", "javascript:window.location.href='" + Common.ResetUrl(kvList) + "'");
        }
    }
    private DataTable GetVote(int id, ref int total, int startIndex)
    {
        DataTable retValue = new DataTable(); 
        retValue.Columns.Add("Id");
        retValue.Columns.Add("Name");
        retValue.Columns.Add("Comment");
        retValue.Columns.Add("Opt");
        retValue.Columns.Add("Point");
        retValue.Columns.Add("Dated");
        Vote[] ms = Vote.GetNominationVotes(id, startIndex, count, ref total);
        if (ms == null)
            return retValue;
        for (int i = 0; i < ms.Length; i++)
        {
            Vote m = (Vote)ms.GetValue(i);
            if (m != null)
            {
                DataRow dr = retValue.NewRow();
                dr["Id"] = m.VoteId;                        // 0
                dr["Name"] = m.Name;                        // 1
                dr["Comment"] = m.Comment;                  // 2
                dr["Opt"] = m.Option;                       // 3
                dr["Point"] = m.Points;                     // 4
                dr["Dated"] = m.VoteDate;                   // 5
                retValue.Rows.Add(dr);
            }
        }
        return retValue;
    }
    protected void dgCommentList_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex >= 0)
        {
            string str = "";
            str = "<table id=\"mrow" + e.Item.Cells[0].Text + "\" cellpadding='0' cellspacing='0' style=\"width:100%;\">";
            str += "<tr>";
            str += "<td style=\"width:100%;background-color: #212121;\"><div style=\"float:left;\">" + Common.GetImageText("user", StringHelper.RestrictMaxChar(e.Item.Cells[1].Text, 10), 15, 15) + "</div><div style=\"float:right;\">" + DateTime.Parse(e.Item.Cells[5].Text).ToShortDateString() + "</div></td>";
            str += "</tr>";
            str += "<tr>";
            string commentText = e.Item.Cells[2].Text;
            StringHelper.ConvertToHtmlText(ref commentText);
            str += "<td><div style=\"float:left; padding:5px; padding-left: 25px;\">" + commentText + "</div></td>";
            str += "</tr>";
            str += "</table>";
            e.Item.Cells[0].Text = str;
        }
        e.Item.Cells[1].Visible = false;
        e.Item.Cells[2].Visible = false;
        e.Item.Cells[3].Visible = false;
        e.Item.Cells[4].Visible = false;
        e.Item.Cells[5].Visible = false;
    }
}
